using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace BouncinBall.Utility
{
    internal static class Helper
    {
        internal static float DotProduct(Vector2 v1, Vector2 v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y;
        }

        internal static float DotProduct(Vector3 v1, Vector3 v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
        }

        //http://www.gamedev.net/topic/393103-c-vector3d-product-implementation/
        public static Vector3 CrossProduct(Vector3 v1, Vector3 v2)
        {
            return new Vector3((v1.Y * v2.Z) - (v2.Y * v1.Z),
                                (v1.Z * v2.X) - (v2.Z * v1.X),
                                (v1.X * v2.Y) - (v2.X * v1.Y));

        }

        internal static Vector3 RotateVector(Matrix matrix, Vector3 vec)
        {
            return new Vector3(DotProduct(matrix.Right, vec), DotProduct(matrix.Up, vec), DotProduct(matrix.Backward, vec));
        }

        internal static Utility.Position FieldPositionFromScreenPosition(Vector2 screenPos, float screenWidth, float screenHeight, float pixPerMeter, float height)
        {
            return new Utility.Position((screenPos.X - screenWidth / 2) / pixPerMeter,
                        (screenPos.Y - screenHeight / 2) / pixPerMeter, height);
        }

        internal static float GetSoundEffectVolume(float zVel, float maxSpeed)
        {
            return Bound(Math.Abs(zVel) / maxSpeed, 0.0F, 0.5F);
        }

        internal static float GetSoundEffectPitch(float zVel, float maxSpeed)
        {
            return Bound(Math.Abs(zVel) / maxSpeed, 0.0F, 1.0F) * -1.0F + 1.0F;
            //if (zVel >= 0)
            //    return Bound(zVel / maxSpeed - 1.0F, -1.0F, 1.0F);
            //else
            //    return Bound(-1 * zVel / maxSpeed + 1.0F, -1.0F, 1.0F);
        }

        private static float Bound(float input, float min, float max)
        {
            if (input > max)
                return max;
            else if (input < min)
                return min;
            else
                return input;
        }
    }
}
